<?php
namespace app\data\model;
use app\index\exception\LogicException;
use think\admin\Model;

/**
 * @name:分组详情
 * @author:Sam
 * @date:2024-04-10
 * @param:[string] param [desc]
*/
class ActTicketGroup extends Model
{
    protected $table = 'act_ticket_group';

     //售卖状态
     public function getSaleStatusAttr()
     {
         $now = time();
         $begin=strtotime($this->group_sale_begin);
         $end = strtotime($this->group_sale_end);
         if($now < $begin){
             //未开始
             return 4;
         }elseif($now > $end){
             //已结束
             return 2;
         }else{
             if($this->rest_quantity=0){
                 //已售完
                 return 3;
             }else{
                 //售卖中
                 return 1;
             }
         }
 
     }

    public function activityModel()
    {
        return $this->belongsTo(ActActivity::class,'activity_id');
    }


    public function orders(){
        return $this->hasMany(ActOrder::class,'group_id');
    }

    public function ticketModel()
    {
        return $this->belongsTo(ActActivityTicket::class,'ticket_id');
    }

    public function ifOnSale()
    {
        $now = time();
        $begin=strtotime($this->group_sale_begin);
        $end = strtotime($this->group_sale_end);
        return $now>=$begin&&$now<=$end;
    }

    public function saleOneTicket()
    {
        if($this->group_remain_number<=0){
            throw new LogicException('门票已售完！');
        }
        $this->group_remain_number --;
        return $this->save();

    }

}